﻿using Microsoft.AspNetCore.Mvc.Filters;
using System.Diagnostics;
using XKJ.OAWeb.Api.Filters;

namespace XKJ.OAWeb.Api.Filters
{
    public class OAwebActionFilter : Attribute,IAsyncActionFilter
    {
        private readonly ILogger<OAwebActionFilter> _logger;

        public OAwebActionFilter(ILogger<OAwebActionFilter> logger)
        {
            _logger = logger;
        }
        //异步Action过滤器
        public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            //判断当前的方法上面与没有[Nlog]这个特征标注，有则不记录日志
            if (context.ActionDescriptor.EndpointMetadata.Any(x=>x.GetType()==typeof(NoLogAttribute)))
            {
                await next();
                return;
            }
            //Stopwatch秒表,统计所用时间
            Stopwatch sw = new Stopwatch();
            //开始计时
            sw.Start();
            //调用Action方法
            await next();
            //拿到接口的路径
            string path = context.HttpContext.Request.Path;
            //停止计时
            sw.Stop();
            //所用的毫秒数
            long n = sw.ElapsedMilliseconds;
            //记录其他信息
            string m = "执行方法用时" + n.ToString();
            //记录Nlog日志
             _logger.LogInformation("m");
        }
    }
}
